package cn.tworice.blog.web;

import cn.tworice.blog.entity.SendMailDto;
import cn.tworice.blog.service.SendCommentService;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/message")
@Api(tags = "留言模块接口")
@CrossOrigin
public class CommentController {


    final SendCommentService sendCommentService;
    @Autowired
    public CommentController(SendCommentService sendCommentService){
        this.sendCommentService=sendCommentService;
    }

    @ApiOperation("留言")
    @PostMapping("leave")
    public RequestResult message(@RequestParam @ApiParam("留言内容") String message,@RequestParam @ApiParam("邮箱") String mail,@RequestParam @ApiParam("验证码") String verify, HttpServletRequest request){
        RequestResult result =sendCommentService.sendMessage(message,mail,verify,request);
        return result;
    }

    @ApiOperation("发送验证邮件")
    @PostMapping("sendMail")
    public RequestResult SendMail(@RequestParam @ApiParam("邮件地址") String mail){
        RequestResult result = new RequestResult();
        try{
            log.info("邮箱-"+mail+"-申请邮箱验证");
            sendCommentService.sendVerify(mail);

            result.setCode(new StatusCode(200,"验证邮件已发送到指定邮箱！"));
            result.appendData("mail",mail);
        }catch (Exception ex){
            System.out.println(ex.getMessage());
            result.setCode(new StatusCode(501,"服务器繁忙，请稍后再试！"));
        }
        return result;
    }

    @ApiOperation("发送普通邮件")
    @PostMapping("admin/replyMail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mail",value = "目标邮箱",dataTypeClass = String.class),
            @ApiImplicitParam(name = "content",value = "邮件内容",dataTypeClass = String.class)
    })
    public RequestResult ReplyMail(String mail,String content){
        RequestResult result = new RequestResult();
        try{
            log.info("邮箱-"+mail+"-申请邮箱验证");
            sendCommentService.sendMail(mail,content);

            result.setCode(new StatusCode(200,"验证邮件已发送到指定邮箱！"));
            result.appendData("mail",mail);
        }catch (Exception ex){
            ex.printStackTrace();
            result.setCode(new StatusCode(501,"服务器繁忙，请稍后再试！"));
        }
        return result;
    }

    @ApiOperation("按页码获取留言列表")
    @GetMapping("admin/messageList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize",value = "每页的数量",defaultValue = "10",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum",value = "要查询的页码",defaultValue = "1",dataTypeClass = Integer.class)
    })
    public RequestResult messageList(@RequestParam(defaultValue = "10",required = false)Integer pageSize,@RequestParam(defaultValue = "1",required = false) Integer pageNum){
        return sendCommentService.messageList(pageSize,pageNum);
    }

    @ApiOperation("获取所有留言")
    @GetMapping("admin/MessageAll")
    public RequestResult messageList(){
        return sendCommentService.messageList();
    }

    @ApiOperation("根据ID删除留言")
    @DeleteMapping("admin/delMessage")
    @ApiImplicitParam(name = "id",value = "ID",dataTypeClass = Integer.class)
    public RequestResult messageDel(Integer id){
        System.out.println(id);
        RequestResult result = new RequestResult();
        try {
            sendCommentService.delMessage(id);
            result.setCode(new StatusCode(200,"删除成功"));
        }catch (Exception ex){
            result.setCode(new StatusCode(500,"删除失败"));
            ex.printStackTrace();
        }

        return result;
    }

}
